package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.drp.dto.BuyListDto;
import com.dy.drp.entity.Buyshorttime;
import com.dy.drp.entity.Cargo;
import com.dy.drp.service.IBuyinfoService;
import com.dy.drp.service.IBuyshorttimeService;
import com.dy.drp.service.ICargoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/buyshorttime")
@Api(tags="采购物品--物品接口")
public class BuyshorttimeController {
    @Autowired
    private IBuyshorttimeService buyshorttimeService;
    @Autowired
    private ICargoService cargoService;
    @Autowired
    private IBuyinfoService buyinfoService;

    @PostMapping("/insertCargo")
    @ApiOperation("添加货物")
    public String insertBuyCargo(Cargo cargo){
        System.out.println(cargo);
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_name",cargo.getCargoName());
        wrapper.eq("cargo_factory",cargo.getCargoFactory());
        Cargo one = cargoService.getOne(wrapper);
        if (one != null){
            Buyshorttime buyshorttime = new Buyshorttime();
            buyshorttime.setCargoId(one.getCargoId());
            buyshorttime.setShortNumber(cargo.getCargoNumber());
            if (buyshorttimeService.save(buyshorttime)){
                return "添加货物成功";
            }
            return "添加货物失败";
        }

        if (cargoService.insertOne(cargo)) {
            Buyshorttime buyshorttime = new Buyshorttime();
            buyshorttime.setCargoId(cargo.getCargoId());
            buyshorttime.setShortNumber(cargo.getCargoNumber());
            if (buyshorttimeService.save(buyshorttime)){
                return "添加货物成功";
            }
            return "添加货物失败";
        }
        return "添加货物失败";
    }

    @GetMapping("/deleteBuyCargo/{cargoName}/{cargoFactory}")
    @ApiOperation("移除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cargoName",value = "货物名"),
            @ApiImplicitParam(name="cargoFactory",value = "厂家")
    })
    public String deleteBuyCargo(@PathVariable("cargoName")String cargoName,
                                  @PathVariable("cargoFactory")String cargoFactory){
        Cargo one = cargoService.getOneBuyNameAndFactory(cargoName, cargoFactory);
        QueryWrapper<Buyshorttime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cargo_id",one.getCargoId());
        if(buyshorttimeService.remove(queryWrapper)){
            return "商品移除成功";
        }else {
            return "商品移除失败";
        }
    }

    @GetMapping("/updateNumber/{cargoName}/{cargoFactory}/{number}")
    @ApiOperation("修改采购货物数量")
    @ApiImplicitParam(name="number",value="采购货物数量")
    public String updateBuyCargoNumber(@PathVariable("cargoName")String cargoName,
                                        @PathVariable("cargoFactory")String cargoFactory,
                                        @PathVariable("number")Long number){
        Cargo one = cargoService.getOneBuyNameAndFactory(cargoName, cargoFactory);
        Buyshorttime buyshorttime = new Buyshorttime();
        buyshorttime.setShortNumber(number);
        QueryWrapper<Buyshorttime> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_id",one.getCargoId());
        if(buyshorttimeService.update(buyshorttime,wrapper)){
            return "修改成功";
        }
        return "修改失败";
    }

    @GetMapping("/insertIntoFactory/{providerName}/{buyPerson}")
    @ApiOperation("入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name="providerName",value="供应商"),
            @ApiImplicitParam(name="buyPerson",value = "采购员")
    })
    public String insertIntoFactory(@PathVariable("providerName")String providerName,
                                     @PathVariable("buyPerson")String buyPerson){
        if(buyinfoService.insertIntoFactory(providerName,buyPerson)){
            return "入库成功";
        }
        return "入库失败";
    }

    @GetMapping("/selectAllBuyShortTime")
    @ApiOperation("获取所有临时采购货物")
    public List<BuyListDto> selectAllBuyShortTime(){
        return buyshorttimeService.selectAllBuyShortTime();
    }
}
